using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Extensions.Configuration;
using System.IO;
using Xunit;

namespace AdminSG3L.ApiTests
{
    public abstract class ApiTestBase : IClassFixture<WebApplicationFactory<Program>>
    {
        protected readonly HttpClient _client;
        protected readonly WebApplicationFactory<Program> _factory;

        protected ApiTestBase()
        {
            _factory = new WebApplicationFactory<Program>()
                .WithWebHostBuilder(builder =>
                {
                    builder.ConfigureAppConfiguration((context, config) =>
                    {
                        var configPath = Path.Combine(AppContext.BaseDirectory, "appsettings.Test.json");
                        if (File.Exists(configPath))
                        {
                            config.AddJsonFile(configPath);
                        }
                        config.AddEnvironmentVariables();
                    });
                });
            
            _client = _factory.CreateClient(new WebApplicationFactoryClientOptions // 这里设置了请求地址
            {
                BaseAddress = new Uri("http://localhost"),
                AllowAutoRedirect = false
            });
        }
    }
}
